package cn.fsw.pethome.pet.service.impl;

import cn.fsw.pethome.pet.domain.PetType;
import cn.fsw.pethome.pet.mapper.PetTypeMapper;
import cn.fsw.pethome.pet.service.IPetTypeService;
import cn.fsw.pethome.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fsw
 * @since 2022-10-23
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> Tree() {
        List<PetType> petTypes = petTypeMapper.selectAll();
        List<PetType> tree = new ArrayList<>();
        for (PetType petType : petTypes) {
            if(petType.getPid() == null){
                tree.add(petType);

            }else {
                for (PetType petType2 : petTypes) {
                    if (petType.getPid() == petType2.getId()){
                        petType2.getChildren().add(petType);
                    }
                }
            }       
        }
        return tree;
    }
}
